package ahl;

import static org.junit.Assert.assertTrue;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;

/**
 * Tests whether or not a robot's gun turns at least once on a robot collision.
 * @author Arthur Lee
 */
public class TestHitRobotGunTurn extends RobotTestBed {
  boolean hitRobot = false;
  boolean afterHitRobot = false;
  boolean turnedGun = false;
  double hitRobotGunHeading, afterHitRobotGunHeading;
  
  /**
   * Specifies that SittingDuck and BattleFire are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,ahl.BattleFire";
  }
  
  /**
   * This test runs for 10 rounds.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 10;
  }
  
  /**
   * Grabs the gun heading before and after a robot collision.
   * @param event The event on an ended turn.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    //Retrieves all snapshots of robots and find if robot collision has occurred.
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    hitRobot = robot.getState().isHitRobot();
    //The turn after the collision occurred.
    if (afterHitRobot) {
      afterHitRobotGunHeading = robot.getGunHeading();
      afterHitRobot = false;
      if (afterHitRobotGunHeading != hitRobotGunHeading) {
        turnedGun = true;
      }
    }
    //The turn on which collision occurred.
    if (hitRobot) {
      hitRobotGunHeading = robot.getGunHeading();
      hitRobot = false;
      afterHitRobot = true;
    }
  }
  
  /**
   * Checks if the gun has turned at least once.
   * @param event The event on an ended battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Check if gun turned at least once after hitting enemy", turnedGun);
  }
  
  /**
   * Specifies where each robot starts off in the field.
   * @return Starting position of robots on the field.
   */
  @Override
  public String getInitialPositions() {
    return "50, 50, 0, 70, 70, 135";
  }
  
  /**
   * Robots will start at the same positions every round.
   * @return The boolean value to determine whether robots should start at same point.
   */
  @Override
  public boolean isDeterministic() {
    return true;
  }
}
